package com.zjw.mybatis.util;

import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtil {

	private static final SqlSessionFactory FACTORY = buildSqlSessionFactory();
	
	private static SqlSessionFactory buildSqlSessionFactory() {
		String myBatisConfigXml = "mybatis-config.xml";
		InputStream is = null;
		try {
			is = Resources.getResourceAsStream(myBatisConfigXml);
			return new SqlSessionFactoryBuilder().build(is);
		} catch (Exception e) {
			System.err.println("Build SqlSessionFactory failed!!!!");
			e.printStackTrace();
			throw new ExceptionInInitializerError(e);
		}
	}
	
	public static SqlSessionFactory getSqlSessionFactory(){
		return FACTORY;
	}

	public static SqlSession openSession() {
		return FACTORY.openSession();
	}
}
